<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="/webjars/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="/static/js/jquery.min.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="/webjars/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <h3>更新</h3>
    <form>
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">id</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="id" th:value="${city.id}">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">name</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="name" th:value="${city.name}">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">countryCode</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="countryCode" th:value="${city.countryCode}">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">district</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="district" th:value="${city.district}">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">population</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="population" th:value="${city.population}">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-2 col-form-label"></label>
            <div class="col-sm-10">
                <input type="button" class="btn btn-primary" value="更新" onclick="update()">
                <input type="button" class="btn btn-info" value="返回" onclick="back()">
            </div>
        </div>
    </form>
</div>
<script th:inline="javascript">
    function update() {
        const city = {
            id: $("#id").val(),
            name: $("#name").val(),
            countryCode: $("#countryCode").val(),
            district: $("#district").val(),
            population: $("#population").val()
        }
        $.ajax({
            type: "POST",
            url: "/updateCity",
            contentType: 'application/json',
            data: JSON.stringify(city),
            success: function (data) {
                alert(data);
                this.window.location.href = "/getOneCity?id=" + $("#id").val();
            }
        });
    }

    function back() {
        this.window.location.href = "/getAll";
    }
</script>
</body>
</html>